#ifndef DBINTERFACE_H
#define DBINTERFACE_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <QVector>
#include <QMap>
#include <optional>
#include "../common/common.h"

class DBInterface:public QObject {
    Q_OBJECT
public:
    DBInterface(QObject *parent = nullptr);
    virtual ~DBInterface();

    // 数据库连接管理
    virtual bool openDatabase(const QString &connectionName = QString()) = 0;
    virtual void closeDatabase() = 0;
    virtual bool isDatabaseOpen() const = 0;
    
    // 数据库初始化
    virtual bool initializeDatabase() = 0;
    virtual bool createTables() = 0;
    
    // 基本CRUD操作
    virtual bool insertMowerInfo(const MowerInfoBase &mowerInfo) = 0;
    virtual bool updateMowerInfo(qint32 id, const MowerInfoBase &mowerInfo) = 0;
    virtual bool deleteMowerInfo(qint32 id) = 0;
    virtual QVector<MowerInfoInDb> selectMowerInfos() = 0;
    virtual MowerInfoInDb selectMowerInfoById(qint32 id) = 0;
    virtual std::optional<MowerInfoInDb> selectMowerInfosBySn(const QString &sn) = 0;
    virtual std::optional<MowerInfoInDb> selectMowerInfosByMac(const QString &wifi_mac) = 0;
    
    // 事务支持
    virtual bool beginTransaction() = 0;
    virtual bool commitTransaction() = 0;
    virtual bool rollbackTransaction() = 0;
    
    // 实用功能
    virtual QString lastError() const = 0;
    virtual bool executeQuery(const QString &query, const QMap<QString, QVariant> &params = QMap<QString, QVariant>()) = 0;
    virtual bool tableExists(const QString &tableName) = 0;

protected:
    QSqlDatabase m_database;
    QString m_connectionName;
};

#endif /* DBINTERFACE_H */